using System;
using System.Linq;
using Coderoom.ScrumPlanner.Data.Repositories;
using Coderoom.ScrumPlanner.Domain;

namespace Coderoom.ScrumPlanner.Tests.FakeRepositories
{
	public sealed class FakeSprintRepository : SprintRepository
	{
		private SprintCollection _data;

		public FakeSprintRepository()
		{
			SetupFakeData();
		}

		public override SprintCollection GetSprints(Guid projectId)
		{
			var returnData = _data.Where(sprint => sprint.ProjectId == projectId);

			var sprints = new SprintCollection();
			foreach (var sprint in returnData)
				sprints.Add(sprint);

			return sprints;
		}

		public override Sprint GetSprint(Guid sprintId)
		{
			return _data.Where(sprint => sprint.Id == sprintId).SingleOrDefault();
		}

		public override void CreateSprint(Sprint sprint)
		{
			sprint.Id = Guid.NewGuid();

			_data.Add(sprint);
		}

		public override bool DeleteSprint(Guid sprintId)
		{
			Sprint sprint;

			if ((sprint = GetSprint(sprintId)) != null)
				return _data.Remove(sprint);

			return false;
		}

		private void SetupFakeData()
		{
			_data = new SprintCollection();

			_data.Add(new Sprint {Id = new Guid("253E448B-FE44-4E03-82A2-10BBBE9FC162"), ProjectId = new Guid("D8185A6C-7145-4751-B149-27B03AB9E5B6"), Title = "Sprint 0"});
			_data.Add(new Sprint {Id = new Guid("01613EB9-85F8-4241-8AE6-B6F2F6168C4A"), ProjectId = new Guid("D8185A6C-7145-4751-B149-27B03AB9E5B6"), Title = "Sprint 1"});
			_data.Add(new Sprint {Id = new Guid("E6EC720B-04DC-4FCD-92E2-B39F7BBB5191"), ProjectId = new Guid("FDB854A9-9D52-414C-83DA-469DEAD2A0D8"), Title = "Sprint 5"});

			foreach (var theme in _data)
				theme.Slug = Entity.CreateSlug(theme.Title);
		}
	}
}